<?php

namespace backend\modules\shop\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\shop\ShopGoods;

/**
 * ShopGoodsSearch represents the model behind the search form about `common\models\shop\ShopGoods`.
 */
class ShopGoodsSearch extends ShopGoods
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'cateid', 'status', 'displayorder', 'total', 'totalcnf', 'sales', 'createtime', 'maxbuy', 'usermaxbuy', 'hasoption', 'dispatch', 'isnew', 'ishot', 'isdiscount', 'isrecommand', 'istime', 'timestart', 'timeend', 'viewcount'], 'integer'],
            [['title', 'thumb', 'mutithumb', 'unit', 'description', 'content', 'goodssn'], 'safe'],
            [['marketprice', 'productprice', 'costprice', 'originalprice', 'weight'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ShopGoods::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'cateid' => $this->cateid,
            'status' => $this->status,
            'displayorder' => $this->displayorder,
            'marketprice' => $this->marketprice,
            'productprice' => $this->productprice,
            'costprice' => $this->costprice,
            'originalprice' => $this->originalprice,
            'total' => $this->total,
            'totalcnf' => $this->totalcnf,
            'sales' => $this->sales,
            'createtime' => $this->createtime,
            'weight' => $this->weight,
            'maxbuy' => $this->maxbuy,
            'usermaxbuy' => $this->usermaxbuy,
            'hasoption' => $this->hasoption,
            'dispatch' => $this->dispatch,
            'isnew' => $this->isnew,
            'ishot' => $this->ishot,
            'isdiscount' => $this->isdiscount,
            'isrecommand' => $this->isrecommand,
            'istime' => $this->istime,
            'timestart' => $this->timestart,
            'timeend' => $this->timeend,
            'viewcount' => $this->viewcount,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'thumb', $this->thumb])
            ->andFilterWhere(['like', 'mutithumb', $this->mutithumb])
            ->andFilterWhere(['like', 'unit', $this->unit])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'goodssn', $this->goodssn]);

        return $dataProvider;
    }
}
